﻿using System;
using System.Collections.Generic;
using System.Data;
using Resources;
using Service;
using Model;


public partial class pcSparepartAdd : System.Web.UI.Page
{
    
    StockService  _StockService = new StockService();
    ReferenceDataDirectService  _ReferenceDirectService = new ReferenceDataDirectService();
    SparepartPurchaseInfo _SparepartInfo = new SparepartPurchaseInfo();
    ReferenceDataService _ReferenceService  = new ReferenceDataService();

    private string PONumber;
    private DateTime purchaseDate;
    private int companyId, typeId, purchasedQuantity, equipmentId;
    string codeNumber;

    protected void Page_Load(object sender, EventArgs e)
    {
                
       
          if(!IsPostBack)
          {
              
                LoadDataToComponent();   

          }else 
        {
            companyId = Int32.Parse(Session["companyId"].ToString());
            purchasedQuantity = Int32.Parse(Session["quantity"].ToString());
            PONumber = Session["poNumber"] != null ? Session["poNumber"].ToString() : string.Empty;
            purchaseDate = DateTime.Parse(Session["purchaseDate"].ToString());
            codeNumber = Session["codeNumber"].ToString();      
        }
          

    }
    private void LoadDataToComponent ()
    {
        /**** 1.test if the code is compose spares part in table [compse]
               a. yes or b.no
               if yes 
            *   1. Get sparepartId from [Compose] by codeNumber
                2. Get sparepart by  sparepartiD == > informationID, modelId from [sparepart]
            *   3. Get sparepart purchase information 
            *   
            * **/
              pnlBody.Visible = false;
              //btnSubmit.Visible = !String.IsNullOrEmpty(Request.QueryString["displayMode"])
              //                ? Request.QueryString["displayMode"].Equals("true") ? false : true
              //                :
              //                    false;
              equipmentId = !String.IsNullOrEmpty(Request.QueryString["equipmentId"])
                          ? Int32.Parse(Request.QueryString["equipmentId"])
                          : 0;
              if (equipmentId == 0) return;
              Equipment equipment = _StockService.GetEquipment(equipmentId);
              if (equipment == null) return;
              EquipPurchaseInfo equipPurchaseInfo = _StockService.GetPurchaseInfo(equipment.PurchaseId);
              if (equipPurchaseInfo == null) return;
              EquipmentTypeSystem equipmentTypeSystem =
                  _ReferenceService.GetEquipmentTypeSystem(equipPurchaseInfo.HaveId);
              if (equipmentTypeSystem == null) return;

            

              // sessions for delete before update
             Session["companyId"] = equipPurchaseInfo.CompanyId;
             Session["poNumber"] = equipPurchaseInfo.POReferenceNumber;
             Session["purchaseDate"] = equipPurchaseInfo.PurchaseDate;
             Session["quantity"] = equipPurchaseInfo.Quantity;
             Session["codeNumber"] = equipment.CodeNumber;
             


          if (_ReferenceDirectService.GetComputerId() == equipmentTypeSystem.EquipmentTypeId)
          {
              pnlBody.Visible = true;
              // work
              IList<SparepartPurchaseInfo> listPCspareparts = _ReferenceDirectService.GetComputerSparepartsList(equipment.CodeNumber);
              foreach (SparepartPurchaseInfo listComputerSpareparts in listPCspareparts)
              {
                  
                  EquipmentTypeSystem equipmentTypeSystemtmp =
                      _ReferenceService.GetEquipmentTypeSystem(
                          listComputerSpareparts.HaveId);


                  
                  if (_ReferenceDirectService.GetHardiskId() == equipmentTypeSystemtmp.EquipmentTypeId)
                  {
                      drlHardisk.SelectedValue = listComputerSpareparts.ModelIdTmp.ToString();
                      txtHardisk.Text = listComputerSpareparts.Capacity.ToString();
                  }
                  if (_ReferenceDirectService.GetNetworkCardId() == equipmentTypeSystemtmp.EquipmentTypeId)
                  {
                      drlNetwork.SelectedValue = listComputerSpareparts.ModelIdTmp.ToString();
                      txtNetwork.Text = listComputerSpareparts.Capacity.ToString();
                  }
                  if (_ReferenceDirectService.GetCPUId() == equipmentTypeSystemtmp.EquipmentTypeId)
                  {
                      drlCPU.SelectedValue = listComputerSpareparts.ModelIdTmp.ToString();
                      txtCPU.Text = listComputerSpareparts.Capacity.ToString();
                  }
                  if (_ReferenceDirectService.GetRamId() == equipmentTypeSystemtmp.EquipmentTypeId)
                  {
                      drlRAM.SelectedValue = listComputerSpareparts.ModelIdTmp.ToString();
                      txtRAM.Text = listComputerSpareparts.Capacity.ToString();
                  }
                  if (_ReferenceDirectService.GetVGAId() == equipmentTypeSystemtmp.EquipmentTypeId)
                  {
                      drlVGA.SelectedValue = listComputerSpareparts.ModelIdTmp.ToString();
                      txtVGA.Text = listComputerSpareparts.Capacity.ToString();
                  }
                  if (_ReferenceDirectService.GetSoundCardId() == equipmentTypeSystemtmp.EquipmentTypeId)
                  {
                      drlSound.SelectedValue = listComputerSpareparts.ModelIdTmp.ToString();

                  }
              }
           }
         
    }
    private Sparepart GetSparePart(int sparepartPurchaseInfoId,int modelId)
    {
        Sparepart equipmentPurchaseInfo = new Sparepart();
        equipmentPurchaseInfo.ModelId = modelId;
        equipmentPurchaseInfo.InformationId = sparepartPurchaseInfoId;
        equipmentPurchaseInfo.IsDamaged = false;
        return equipmentPurchaseInfo;

    }
     private SparepartPurchaseInfo GetSparePartPurchaseInfo(int typeId, float capacity)
    {
        SparepartPurchaseInfo equipmentPurchaseInfo = new SparepartPurchaseInfo();
        
        equipmentPurchaseInfo.CompanyId = companyId;
        equipmentPurchaseInfo.POReferenceNumber = PONumber;
        equipmentPurchaseInfo.PurchaseDate = purchaseDate;
        equipmentPurchaseInfo.Quantity = 1;
        equipmentPurchaseInfo.StartDate = DateTime.Parse(Resource.EmptylDateTime); ;
        equipmentPurchaseInfo.EndDate = DateTime.Parse(Resource.EmptylDateTime);
        equipmentPurchaseInfo.Capacity = capacity;
        // get haveid, suppose systemid  = 3 (other )
         int defaultSystem = _ReferenceDirectService.GetDefaultSystemId();
        EquipmentTypeSystem typeSystem = _ReferenceService.GetEquipmentTypeSystem(typeId,defaultSystem );
        if (typeSystem == null)
        {
            typeSystem  = new EquipmentTypeSystem();
            typeSystem.EquipmentTypeId = typeId;
            typeSystem.SystemId = defaultSystem;
            _ReferenceService.InsertEquipmentTypeSystem(typeSystem);
            
        }
        
        equipmentPurchaseInfo.HaveId = typeSystem.EquipmentTypeSystemId;
        return equipmentPurchaseInfo;

    }
     private void GenerateTypeModel(int typeId,int modelId)
     {
         EquipmentTypeModel typeModel = _ReferenceService.GetEquipmentTypeModel(typeId, modelId);
         if (typeModel == null)
         {
             typeModel = new EquipmentTypeModel();
             typeModel.ModelId = modelId;
             typeModel.TypeId = typeId;
             _ReferenceService.InsertEquipmentTypeModel(typeModel);
         }
     }
    
    
protected void  btnSubmit_Click(object sender, EventArgs e)
{
    if (!new Utility().TestPermissions(Resource.PermissionAddPcSparepart))
    {
        lblMessageValidate.Text = Resource.Permission;
        pnlBody.Visible = false;
        return;
    }

    //test validatity of spare part 
    // companyID,ponumber 
    if (String.IsNullOrEmpty(PONumber)) return;
    if (companyId == 0) return;
    EquipmentSparepart equipmentSparepart;

    //delete and add new  from compose(codenumber),from sparepartinfo(informationid), from sparepart(i)
     _StockService.DeleteSparepartCompose(codeNumber);
     IList<SparepartPurchaseInfo> listPCspareparts = _ReferenceDirectService.GetComputerSparepartsList(codeNumber);
    foreach (SparepartPurchaseInfo pcSparepart in listPCspareparts)
    {
        _StockService.DeleteSparepart(pcSparepart.SparepartPurchaseInfoId);
        _StockService.DeleteSparepartInfo(pcSparepart.SparepartPurchaseInfoId);
    }
     

    // add all spare part of computers
    for (int i = 0; i < purchasedQuantity; i++)
    {
        //VGA
        if (drlVGA.SelectedIndex > 0)
        {
            _SparepartInfo = GetSparePartPurchaseInfo(_ReferenceDirectService.GetVGAId(),
                                                      !String.IsNullOrEmpty(txtVGA.Text) ? float.Parse(txtVGA.Text) : 0);
             GenerateTypeModel(_ReferenceDirectService.GetVGAId(), Int32.Parse(drlVGA.SelectedValue));
             
            _StockService.InsertSparepartInfo(_SparepartInfo);

            equipmentSparepart = GetEquipmentSparepart(_SparepartInfo.SparepartPurchaseInfoId);
            _StockService.SaveEquipmentSparepart(equipmentSparepart);

            _StockService.InsertSparepart(GetSparePart(_SparepartInfo.SparepartPurchaseInfoId, Int32.Parse(drlVGA.SelectedValue)));
        }
        //CPU
        if (drlCPU.SelectedIndex > 0)
        {
            GenerateTypeModel(_ReferenceDirectService.GetCPUId(), Int32.Parse(drlCPU.SelectedValue));
            _SparepartInfo = GetSparePartPurchaseInfo(_ReferenceDirectService.GetCPUId(),
                                                      !String.IsNullOrEmpty(txtCPU.Text) ? float.Parse(txtCPU.Text) : 0);
            _StockService.InsertSparepartInfo(_SparepartInfo);
            equipmentSparepart = GetEquipmentSparepart(_SparepartInfo.SparepartPurchaseInfoId);

            _StockService.SaveEquipmentSparepart(equipmentSparepart);
            _StockService.InsertSparepart(GetSparePart(_SparepartInfo.SparepartPurchaseInfoId,Int32.Parse(drlCPU.SelectedValue)));
        }
        //HARDISK
        if (drlHardisk.SelectedIndex > 0)
        {
            GenerateTypeModel(_ReferenceDirectService.GetHardiskId(), Int32.Parse(drlHardisk.SelectedValue));
            _SparepartInfo = GetSparePartPurchaseInfo(_ReferenceDirectService.GetHardiskId(),
                                                      !String.IsNullOrEmpty(txtHardisk.Text)
                                                          ? float.Parse(txtHardisk.Text)
                                                          : 0);
            equipmentSparepart = GetEquipmentSparepart(_SparepartInfo.SparepartPurchaseInfoId);
            equipmentSparepart.InformationId = _SparepartInfo.SparepartPurchaseInfoId;
            _StockService.SaveEquipmentSparepart(equipmentSparepart);
            _StockService.InsertSparepart(GetSparePart(_SparepartInfo.SparepartPurchaseInfoId, Int32.Parse(drlHardisk.SelectedValue)));
        }
               

        //Network
        if (chkNetwork.Checked)
            if (drlNetwork.SelectedIndex > 0)
            {
                GenerateTypeModel(_ReferenceDirectService.GetNetworkCardId(), Int32.Parse(drlNetwork.SelectedValue));
                _SparepartInfo = GetSparePartPurchaseInfo(_ReferenceDirectService.GetNetworkCardId(),
                                                          !String.IsNullOrEmpty(txtNetwork.Text)
                                                              ? float.Parse(txtNetwork.Text)
                                                              : 0);

                _StockService.InsertSparepartInfo(_SparepartInfo);

                equipmentSparepart = GetEquipmentSparepart(_SparepartInfo.SparepartPurchaseInfoId);
                _StockService.SaveEquipmentSparepart(equipmentSparepart);

                _StockService.InsertSparepart(GetSparePart(_SparepartInfo.SparepartPurchaseInfoId, Int32.Parse(drlNetwork.SelectedValue)));
            }

        //RAm
        if (drlRAM.SelectedIndex > 0)
        {
            GenerateTypeModel(_ReferenceDirectService.GetRamId(), Int32.Parse(drlRAM.SelectedValue));
            _SparepartInfo = GetSparePartPurchaseInfo(_ReferenceDirectService.GetRamId(),
                                                  !String.IsNullOrEmpty(txtRAM.Text) ? float.Parse(txtRAM.Text) : 0);

            _StockService.InsertSparepartInfo(_SparepartInfo);

            equipmentSparepart = GetEquipmentSparepart(_SparepartInfo.SparepartPurchaseInfoId);
            _StockService.SaveEquipmentSparepart(equipmentSparepart);

            _StockService.InsertSparepart(GetSparePart(_SparepartInfo.SparepartPurchaseInfoId, Int32.Parse(drlRAM.SelectedValue)));
        }
        // insert computer info : ip , name in computer info
        ComputerInfo pc = new ComputerInfo();
        pc.ComputerInfoId = _ReferenceDirectService.GetComputerCodeNumberLastId()+1;
        pc.ComputerName = txtPCName.Text;
        pc.IPAdress = txtIP.Text;
        _StockService.SaveComputerInfo(pc);
        }
    }
    private EquipmentSparepart GetEquipmentSparepart(int sparepartPurchaseInfoId)
    {
        EquipmentSparepart equipmentSparepart = new EquipmentSparepart();
        equipmentSparepart.EquipmentId = !String.IsNullOrEmpty(Request.QueryString["equipmentId"])
                                ? Int32.Parse(Request.QueryString["equipmentId"])
                                : 0;
        equipmentSparepart.InformationId = sparepartPurchaseInfoId;
        equipmentSparepart.CreateDate = !String.IsNullOrEmpty(Request.QueryString["purchaseDate"].ToString())
                           ? DateTime.Parse(Request.QueryString["purchaseDate"].ToString())
                           : DateTime.Parse(Resource.EmptylDateTime);
        equipmentSparepart.Description = (string)Session["description"];
        return equipmentSparepart;

    }

    private void SetEnableMode()
    {
        drlNetwork.Enabled = chkNetwork.Checked;
        txtNetwork.Enabled = chkNetwork.Checked;
    }
    protected void chkNetwork_CheckedChanged(object sender, EventArgs e)
    {
        SetEnableMode();
    }
}
